let $table;

function tishiEmpty() {
    return "查询不到相关数据哦亲！";
}

$(function () {
    let basePath = $('#path').val();
    $table = $('#' + table_name);
    _table = $table.dataTable(
        $.extend(true, {}, CONSTANT.DATA_TABLES.DEFAULT_OPTION, {
            ajax: function (data, callback, settings) {// ajax配置为function,手动调用异步查询
                let param = dataManage.getQueryCondition(data);
                $.ajax({
                    type: "POST",
                    url: basePath + table_data_url,
                    cache: false,
                    data: param,
                    contentType: 'application/json;charset=UTF-8',
                    dataType: "json",
                    success: function (result) {
                        if (isNotNull(result) && result.code === 40003) {
                            wrong_msg("没有权限");
                            return false;
                        }

                        if (isNull(result) || result.code !== 2000) {
                            console.error("请求结果出现错误：", result.msg);
                            wrong_msg(result.msg);
                            return false;
                        }

                        $("#select-all").attr("checked", false);
                        let returnData = {};
                        returnData.draw = data.draw;
                        returnData.recordsTotal = result.recordsTotal;
                        returnData.recordsFiltered = result.recordsTotal;
                        returnData.data = result.data;
                        if (result.extParam) {
                            returnData.extParam = result.extParam;
                        }
                        callback(returnData);
                        foot_info(result);
                    },
                    error: function (XMLHttpRequest, textStatus,
                                     errorThrown) {
                        //alert("查询失败");
                    }
                });
            },
            columns: returnData.getDate,
            dom: number_list,
            createdRow: function (row, data, index) {
                createdRow(row, data, index)
            },
            drawCallback: function (settings) {
                drawCallback(settings);
            },
            bProcessing: false,
            oLanguage: {
                sEmptyTable: tishiEmpty()
            },
            fnDrawCallback: function (table) {
                let tableId = $(this).attr('id');
                $('#' + tableId + '_paginate').append("<div>到第 <input type='text' id='changePage' class='input-text' style='width:50px;height:27px'> 页 " +
                    "<a class='btn btn-default shiny' href='javascript:void(0);' id='dataTable-btn' style='text-align:center'>确认</a></div>");
                let oTable = $('#' + tableId).dataTable();
                $('#dataTable-btn').click(function (e) {
                    let redirectpage;
                    if ($("#changePage").val() && $("#changePage").val() > 0) {
                        redirectpage = $("#changePage").val() - 1;
                    } else {
                        redirectpage = 0;
                    }
                    oTable.fnPageChange(redirectpage);
                });
                fn_draw_call_back();
            }
        })).api();
    $("#find").click(function () {
        find();
        $("#select-all").attr("checked", false);
    });

    _table.on('order.dt search.dt draw.dt', function () {
        let page_info = $table.DataTable().page.info()
        _table.column(0, {search: 'applied', order: 'applied'}).nodes().each(function (cell, i) {
            cell.innerHTML = i + 1 + page_info.start;
        });
    }).draw();
});

// 表格重新加载
function find() {
    dataManage.fuzzySearch = false;
    _table.draw();
}

//页面上点击此属性，将当前页的列表数据全部选中
$('#select-all').on('click', function () {
    if (this.checked) {
        $('.iCheck').each(function () {
            this.checked = true;
        });
    } else {
        $('.iCheck').each(function () {
            this.checked = false;
        });
    }
});
